package com.zjitc.P2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/13 15:06
 * @description:
 */
public class AProxy implements InvocationHandler {


  private M target;


  public AProxy(M target) {
    this.target = target;
  }


  // AProxy.newProxyInstance() 返回一个代理对象
  // every time 这个代理对象待用这个接口的任意方法时，都会调用invoke方法
  public Object getProxy(){
    Class clazz = target.getClass();
    return Proxy
        .newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
  }


  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if(method.getName() == "a"){
      System.out.println("init");
      method.invoke(target,args);
    }else{
      method.invoke(target,args);
    }
    return null;
  }
}
